#if !defined(_CAAUDIO_H_)
#define _CAAUDIO_H_

#include "AObject.h"
class CAXMLNode;

class CAAudio : public CAObject
{
protected:
	class CASoundItem : public CAObject
	{
	public:
		CASoundItem() { autorelease(); }
		CAString id;
		CAString file;
	};
	class CAMusicItem : public CASoundItem
	{
	public:
		//NA if sound, A if music
		float playtime;
		float duration;
	};

	CATimeProvider* _timer;

	CCArray* _aryMusic;
	int _state;
	ccTime _stateTime;
	void _setState(int st);

	CAMusicItem* _playing;
	CAMusicItem* _toplay;
	bool _fadeoutMusic(float intime);
	bool _fadeinMusic(float intime);
	CASoundItem* _findItem(CCArray* pary, const char* psz, int* pnIndex = null);

	CCArray* _arySound;

	float _volumeMusic;
	float _volumeSound;

	float _FADE_SWITCH;
	float _FADE_PAUSE;
	float _FADE_RESUME;
public:
	CAAudio(CATimeProvider* ptp);
	virtual ~CAAudio(void);

	void loadMusicItem(CAXMLNode* pnode);
	void loadSoundItem(CAXMLNode* pnode);

	void setFadeTimeSwitch(float v) { _FADE_SWITCH = v ; }
	void setFadeTimePause(float v)	{ _FADE_PAUSE = v ; }
	void setFadeTimeResume(float v) { _FADE_RESUME = v ; }

	void switchMusic(const char* pszMusic);
	void playMusic();
	void stopMusic();
	void pauseMusic();
	void resumeMusic();

	bool isMusicMute() const { return _volumeMusic <= 0.0f; };
	bool isSoundMute() const { return _volumeSound <= 0.0f; };
	void enableMusic(bool e);
	void enableSound(bool e);

	void playSound(const char* pszSound, float volume = 1.0f);

	void update();
};

#endif //_CAAUDIO_H_